import http from "@/utils/http";
import { HttpResponse, checkHttpError } from "../common";
import { FORM_DATA_AXIOS_CONFIG } from "../constants";

/**
 * 获得所有公司列表
 */
export async function getCompanyList() {
  const resp: HttpResponse<Company[]> = await http.get(
    "/Company/listcompany2/"
  );
  return checkHttpError(resp) ? resp.detail : false;
}

/**
 * 显示公司内部关系树
 */
export async function getCompanyDepTree(id: string) {
  const resp: HttpResponse<CompanyDependency[]> = await http.get(
    "/Department/showdeptree2/?shortcpname__id=" + id
  );

  return checkHttpError(resp) ? resp.detail : false;
}

/**
 * 获得公司详细信息
 */
export async function getCompanyData(id: string) {
  const resp: HttpResponse<Company> = await http.get(
    `/Company/${id}/showcompany/`
  );

  return checkHttpError(resp) ? resp.detail : false;
}

/**
 * 获得部门信息
 */
export async function getDepartmentData(id: string) {
  const resp: HttpResponse<Department> = await http.get(
    `/Department/${id}/showdepid/`
  );

  return checkHttpError(resp) ? resp.detail : false;
}

/**
 * 创建部门
 */
export async function createDepartment({
  deptname,
  shortcpname,
  pdeptid,
  contactor,
  contactway,
  remark
}) {
  const form = new FormData();

  form.append("deptname", deptname);
  form.append("shortcpname", shortcpname);
  form.append("pdeptid", pdeptid);
  form.append("contactor", contactor);
  form.append("contactway", contactway);
  form.append("remark", remark);

  const resp: HttpResponse<Department> = await http.post(
    "/Department/createdepartment2/",
    form,
    FORM_DATA_AXIOS_CONFIG
  );

  return checkHttpError(resp) ? resp.detail : false;
}

export async function updateDepartment({
  id,
  deptname,
  shortcpname,
  pdeptid,
  contactor,
  contactway,
  remark
}) {
  const form = new FormData();

  form.append("deptname", deptname);
  form.append("shortcpname", shortcpname);
  form.append("pdeptid", pdeptid);
  form.append("contactor", contactor);
  form.append("contactway", contactway);
  form.append("remark", remark);

  const resp: HttpResponse = await http.post(
    `/Department/${id}/updatedepid3/`,
    form,
    FORM_DATA_AXIOS_CONFIG
  );

  return checkHttpError(resp) ? resp.detail : false;
}

/**
 * 删除部门
 */
export async function deleteDepartment(id: string) {
  const resp: HttpResponse<string> = await http.get(
    `/Department/${id}/deletedepid2/`
  );
  return checkHttpError(resp) ? resp.detail : false;
}

/**
 * 公司数据结构
 */
export type Company = {
  aaddress: string;
  ccity: {
    id: number;
    name: string;
    province: {
      id: number;
      name: string;
    };
  };
  ccompany: string;
  companytype: number;
  contacter: string;
  contactway: string;
  id: string;
  isdel: number;
  ppostcode: string;
  remark: string;
};

export type CompanyDependency = {
  id: string;
  // 是否是父节点
  isParent: boolean;
  name: string;
  // 父节点
  pId: string | 0;
};

export type Department = {
  contactor: string;
  contactway: string;
  deptname: string;
  deptype: number;
  id: string;
  isdel: string;
  remark: string;
  pdeptid?: Department;
  shortcpname: Company;
};
